﻿using com.nm;
using com.nm.library.Excels;
using com.nm.library.Helper;
using NHibernateVertica;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using VerticaAPI.model;

namespace VerticaAPI.Export
{
    /// <summary>
    /// 端头陈列量设置
    /// </summary>
    [Export(typeof(ExcelImport))]
    public class MatSuppleShopExport : ExcelImport
    {
        private static int num = 0;
        private static string ExcelName = "店铺限制补货";
        public override ExcelImportType Type => ExcelImportType.MATSUPPLESHOP;

        public override Dictionary<string, ImportVerify> DictFields => dictFields;
        /// <summary>
        /// Excel字段映射
        /// </summary>
        #region Excel字段映射
        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="货号",FieldName="MaterialID",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true) },
            new ImportVerify{ ColumnName="所有店铺限制补货",FieldName="AllShopFlag",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="店铺编号",FieldName="ShopId",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="店铺名称",FieldName="ShopName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,50,false,true)}

           }.ToDictionary(e => e.ColumnName, e => e);
        #endregion


        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="Keys"></param>
        /// <returns></returns>
        public override DataTable GetExportData(string Keys)
        {
            var helper = new VerticaDataHelper();
            var list = new List<MatSuppleShopModel.MatSuppleShopImportModel>();
            var dt = DataTableHelper.LinqToDataTable(list);
            return dt;

        }

        /// <summary>
        /// 保存导入数据方法
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="extraInfo"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            string msg = string.Empty;
            var result = new ImportResult
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
            };
            try
            {
                var handler = new MatSuppleShopHandler();
                var list = ModelConvertHelper<MatSuppleShopModel.MatSuppleShopImportModel>.ConvertToModel(dt);
                var err_dt = handler.ChangeList(list);
                if (err_dt != null)
                {
                    var err_count = err_dt.Rows.Count;
                    var old_count = dt.Rows.Count;
                    result.Message = $"成功导入{old_count}";
                    if (err_count > 0)
                    {
                        result.Message = $"成功导入{(old_count - err_count)}条数据{(err_count == 0 ? "" : $@",有{err_count}条错误数据")}";
                        var path = ExcelImporMapper.GetTemplatePath(Type);
                        result.Url = ExcelUtil.ExportErrorExeclToDB(err_dt, path, dictFields, ExcelName, num);
                        num++;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error($@"导入{ExcelName}：" + ex);
                result.IsSuccess = false;
                result.ExtraInfo = ex;
                result.Message = ex.Message;
            }
            return result;

        }
    }
}
